﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using Mes.Pendant.ArrivalNotices.Dto;
using Mes.Pendant.Common.DtoMapping;
using Mes.Pendant.MaterialManagements;
using Mes.Pendant.MaterialManagementService.Dto;
using Mes.Pendant.NotificationArrivals;
using Mes.Pendant.ProcurementWarehousings;
using Mes.Pendant.ProductionWorkOrders;
using Mes.Pendant.ProductionWorkOrderService;
using Mes.Pendant.ProductionWorkOrderService.Dto;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace Mes.Pendant.ProductionWorkOrderServicess
{
	public class ProductionWorkOrderServices : ApplicationService, IProductionWorkOrderService
    {
        private readonly IRepository<ProductionWorkOrder, int> productionWorkOrderRepository;
        private readonly IRepository<MaterialManagement, int> materialManagementRepository;
        private readonly IMapper mapper;

        public ProductionWorkOrderServices(IRepository<ProductionWorkOrder, int> productionWorkOrderRepository, IRepository<MaterialManagement, int> materialManagementRepository, IMapper mapper)
        {
            this.productionWorkOrderRepository = productionWorkOrderRepository;
            this.materialManagementRepository = materialManagementRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 添加生产工单信息
        /// </summary>
        /// <param name="insertProductdto"></param>
        /// <returns></returns>
        public async Task<int> AddProducorders(InsertProductdto insertProductdto)
        {
            var productdto=insertProductdto.MapTo<ProductionWorkOrder>();
            await productionWorkOrderRepository.InsertAsync(productdto);
            return 1;
        }

        /// <summary>
        /// 获取生产工单数据
        /// </summary>
        /// <param name="workOrderCode"></param>
        /// <param name="workOrderName"></param>
        /// <param name="index"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<PagedResult> GetProductdatas(string? workOrderCode, string? workOrderName, int index = 1, int limit = 10)
        {
            var predicate = PredicateBuilder.New<ProductionWorkOrder>(true);
            if (!string.IsNullOrEmpty(workOrderCode))
            {
                predicate=predicate.And(m=>m.WorkOrderCode.Contains(workOrderCode));
            }
            if (!string.IsNullOrEmpty(workOrderName))
            {
                predicate=predicate.And(m=>m.WorkOrderName.Contains(workOrderName));
            }
            var productList = productionWorkOrderRepository.GetListAsync(predicate).Result;
            PagedResult pagedResult = new PagedResult();
           pagedResult.CurrentPage = index;
            pagedResult.PageSize = limit;
            pagedResult.RowCount= productList.Count();
            pagedResult.PageCount = (int)Math.Ceiling((pagedResult.RowCount) / (double)limit);
            pagedResult.Queryable=productList.OrderBy(x=>x.Id).Skip((index-1)*limit).Take(limit).AsQueryable();
            pagedResult.Queryable=pagedResult.Queryable.MapToList<SearchProductdto>().AsQueryable();
            return pagedResult;
        }

        /// <summary>
        /// 获取物料数据
        /// </summary>
        /// <returns></returns>
        public async Task<List<InsertMaterialMangementdto>> GetMaterials()
        {
            var list = materialManagementRepository.GetListAsync().Result.AsQueryable();
            var result = mapper.Map<List<InsertMaterialMangementdto>>(list);
            return result;
        }

        /// <summary>
        /// 根据物料编号查询数据
        /// </summary>
        /// <param name="materialCode"></param>
        /// <returns></returns>
        public async Task<InsertMaterialMangementdto> GetMaterialMangement(string materialCode)
        {
            var list = materialManagementRepository.FirstOrDefaultAsync(m => m.MaterialCode == materialCode).Result ;
            var result = list.MapTo<InsertMaterialMangementdto>();
            return result;
        }
    }
}
